import { hotList, soonList } from '@/api/film'

const getDefaultState = () => {
  return {
    allFilmList: [],
    hotFilmList: [],
    soonFilmList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_ALL_FILM: (state, list) => {
    state.allFilmList = list || []
  },
  SET_HOT_FILM: (state, list) => {
    state.hotFilmList = list || []
  },
  SET_SOON_FILM: (state, list) => {
    state.soonFilmList = list || []
  },
}

const actions = {
  getFilmList({ commit, rootGetters }) {
    const cityId = rootGetters['currentCity'].cityId
    return Promise.all([
      hotList({ cityId }),
      soonList({ cityId })
    ]).then(([{ data: hot }, { data: soon }]) => {
      commit('SET_HOT_FILM', hot.list)
      commit('SET_SOON_FILM', soon.list)
      const allList = hot.list.concat(soon.list)
      commit('SET_ALL_FILM', allList)
      return allList
    })
  },

  // remove token
  resetFilm({ commit }) {
    return new Promise(resolve => {
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

